Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 464)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 12.85081 12.85666 12.86245 12.86817 12.87381 12.87936 12.88483 12.89019
## [9] 12.89544 12.90059 12.90560 12.91049 12.91525 12.91986 12.92432 12.92862
## [17] 12.93276 12.93672 12.94051 12.94411 12.94751 12.95072 12.95373 12.95656
## [25] 12.95922 12.96172 12.96406 12.96625 12.96831 12.97022 12.97202 12.97369
## [33] 12.97525 12.97671 12.97807 12.97934 12.98053 12.98164 12.98268 12.98367
## [41] 12.98460 12.98548 12.98633 12.98706 12.98760 12.98795 12.98812 12.98813
## [49] 12.98799 12.98769 12.98725 12.98667 12.98597 12.98516 12.98424 12.98322
## [57] 12.98211 12.98092 12.97966 12.97833 12.97695 12.97552 12.97405 12.97255
## [65] 12.97103 12.96950 12.96796 12.96643 12.96491 12.96341 12.96194 12.96018
## [73] 12.95782 12.95492 12.95153 12.94769 12.94346 12.93889 12.93403 12.92893
## [81] 12.92364 12.91821 12.91269 12.90713 12.90159 12.89611 12.89075 12.88555
## [89] 12.88057 12.87585 12.87145 12.86742 12.86285 12.85688 12.84963 12.84122
## [97] 12.83176 12.82139 12.81020 12.79833 12.78590 12.77301 12.75980 12.74637
## [105] 12.73285 12.71936 12.70601 12.69292 12.68022 12.66802 12.65644 12.64560
## [113] 12.63561 12.62660 12.61869 12.61199 12.60663 12.60271 12.60037 12.59971
## [121] 12.60082 12.60356 12.60775 12.61322 12.61978 12.62726 12.63546 12.64422
## [129] 12.65335 12.66266 12.67199 12.68115 12.68996 12.69824 12.70580 12.71364
## [137] 12.72273 12.73290 12.74400 12.75584 12.76827 12.78110 12.79418 12.80734
## [145] 12.82039 12.83319 12.84555 12.85731 12.86830 12.87883 12.88934 12.89984
## [153] 12.91035 12.92090 12.93150 12.94217 12.95292 12.96378 12.97477 12.98590
## [161] 12.99719 13.01158 13.03138 13.05570 13.08361 13.11423 13.14664 13.17993
## [169] 13.21321 13.24556 13.27608 13.30386 13.32800 13.34759 13.36173 13.37373
## [177] 13.38733 13.40227 13.41831 13.43518 13.45263 13.47041 13.48826 13.50593
## [185] 13.52316 13.53970 13.55528 13.56967 13.58259 13.59381 13.60305 13.61008
## [193] 13.61462 13.61643 13.61526 13.61084 13.60294 13.59176 13.57771 13.56121
## [201] 13.54266 13.52247 13.50106 13.47884 13.45622 13.43361 13.41142 13.39007
## [209] 13.36995 13.35150 13.33208 13.30931 13.28392 13.25665 13.22822 13.19939
## [217] 13.17087 13.14342 13.11775 13.09066 13.05889 13.02318 12.98424 12.94279
## [225] 12.89955 12.85525 12.81060 12.76633 12.72315 12.68179 12.64296 12.60740
## [233] 12.57581 12.54436 12.50926 12.47135 12.43142 12.39031 12.34883 12.30780
## [241] 12.26803 12.23035 12.19557 12.16451 12.13799 12.11426 12.09109 12.06854
## [249] 12.04668 12.02556 12.00526 11.98584 11.96736 11.94990 11.93350 11.91825
## [257] 11.90421 11.89143 11.87999 11.87031 11.86255 11.85635 11.85136 11.84726
## [265] 11.84368 11.84028 11.83671 11.83263 11.82999 11.83062 11.83403 11.83971
## [273] 11.84716 11.85587 11.86534 11.87507 11.88456 11.89330 11.90078 11.90651
## [281] 11.90998 11.91069 11.91337 11.92225 11.93604 11.95345 11.97320 11.99400
## [289] 12.01457 12.03363 12.04990 12.06208 12.06889 12.06905 12.06444 12.05794
## [297] 12.04976 12.04012 12.02922 12.01727 12.00450 11.99111 11.97731 11.96331
## [305] 11.94933 11.93557 11.92226 11.90959 11.89410 11.87312 11.84813 11.82060
## [313] 11.79201 11.76382 11.73751 11.71454 11.69639 11.67912 11.65820 11.63428
## [321] 11.60798 11.57993 11.55076 11.52110 11.49157 11.46282 11.43546 11.41012
## [329] 11.38744 11.36805 11.35257 11.33774 11.32035 11.30110 11.28069 11.25980
## [337] 11.23916 11.21944 11.20136 11.18561 11.17288 11.16389 11.15932 11.15806
## [345] 11.15846 11.16043 11.16388 11.16874 11.17490 11.18229 11.19081 11.20038
## [353] 11.21091 11.22231 11.23450 11.24739 11.26088 11.27669 11.29605 11.31819
## [361] 11.34234 11.36774 11.39361 11.41918 11.44369 11.46637 11.48928 11.51485
## [369] 11.54282 11.57293 11.60492 11.63853 11.67350 11.70957 11.74648 11.78398
## [377] 11.82181 11.85970 11.89740 11.93465 11.97119 12.00676 12.04109 12.07395
## [385] 12.10505 12.13903 12.17944 12.22442 12.27213 12.32071 12.36832 12.41308
## [393] 12.45316 12.48669 12.51755 12.55048 12.58501 12.62065 12.65692 12.69332
## [401] 12.72937 12.76459 12.79849 12.83059 12.86039 12.88741 12.91116 12.93117
## [409] 12.94881 12.96572 12.98181 12.99698 13.01113 13.02415 13.03596 13.04645
## [417] 13.05553 13.06309 13.06903 13.07327 13.07606 13.07775 13.07832 13.07775
## [425] 13.07603 13.07315 13.06909 13.06383 13.05737 13.04968 13.04076 13.03059
## [433] 13.01915 13.00644 12.99243 12.97726 12.96103 12.94370 12.92521 12.90549
## [441] 12.88449 12.86216 12.83844 12.81338 12.78706 12.75949 12.73065 12.70057
## [449] 12.66923 12.63664 12.60280 12.56771 12.53138 12.49379 12.45496 12.41489
## [457] 12.37357 12.33101 12.28720 12.24216 12.19587 12.14835 12.09959 12.04960
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 464)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.51253 12.51355 12.51456 12.51555 12.51652 12.51749 12.51847 12.51944
## [9] 12.52043 12.52144 12.52248 12.52354 12.52464 12.52578 12.52697 12.52821
## [17] 12.52951 12.53088 12.53231 12.53383 12.53543 12.53711 12.53887 12.54067
## [25] 12.54252 12.54439 12.54630 12.54823 12.55017 12.55213 12.55408 12.55604
## [33] 12.55800 12.55994 12.56186 12.56376 12.56562 12.56746 12.56925 12.57099
## [41] 12.57268 12.57431 12.57588 12.57740 12.57888 12.58033 12.58175 12.58314
## [49] 12.58451 12.58585 12.58718 12.58849 12.58978 12.59106 12.59232 12.59358
## [57] 12.59484 12.59609 12.59733 12.59858 12.59983 12.60109 12.60236 12.60363
## [65] 12.60492 12.60622 12.60754 12.60888 12.61024 12.61163 12.61304 12.61462
## [73] 12.61651 12.61868 12.62108 12.62370 12.62651 12.62946 12.63255 12.63573
## [81] 12.63897 12.64225 12.64554 12.64880 12.65201 12.65514 12.65815 12.66102
## [89] 12.66373 12.66623 12.66850 12.67051 12.67127 12.66995 12.66670 12.66168
## [97] 12.65506 12.64700 12.63767 12.62722 12.61582 12.60363 12.59081 12.57754
## [105] 12.56397 12.55026 12.53658 12.52309 12.50995 12.49733 12.48539 12.47429
## [113] 12.46419 12.45527 12.44768 12.44158 12.43714 12.43451 12.43388 12.43539
## [121] 12.43860 12.44290 12.44818 12.45434 12.46128 12.46889 12.47708 12.48574
## [129] 12.49478 12.50407 12.51354 12.52306 12.53255 12.54394 12.55888 12.57686
## [137] 12.59735 12.61983 12.64377 12.66864 12.69393 12.71911 12.74365 12.76704
## [145] 12.78874 12.80823 12.82499 12.84278 12.86488 12.89012 12.91730 12.94524
## [153] 12.97275 12.99864 13.02172 13.04080 13.05890 13.07952 13.10218 13.12642
## [161] 13.15175 13.17770 13.20379 13.22955 13.25450 13.27817 13.30009 13.31977
## [169] 13.33674 13.35054 13.36322 13.37703 13.39174 13.40712 13.42295 13.43898
## [177] 13.45500 13.47077 13.48606 13.50065 13.51431 13.52680 13.53790 13.54737
## [185] 13.55500 13.56054 13.56377 13.56446 13.56239 13.55779 13.55116 13.54268
## [193] 13.53251 13.52080 13.50771 13.49342 13.47807 13.46184 13.44488 13.42737
## [201] 13.40945 13.39129 13.37305 13.35490 13.33699 13.31741 13.29448 13.26875
## [209] 13.24075 13.21102 13.18011 13.14855 13.11688 13.08564 13.05537 13.02661
## [217] 12.99990 12.97159 12.93820 12.90057 12.85951 12.81584 12.77040 12.72399
## [225] 12.67746 12.63161 12.58727 12.54527 12.50642 12.47155 12.44149 12.41095
## [233] 12.37521 12.33591 12.29473 12.25330 12.21329 12.17635 12.14413 12.11828
## [241] 12.09558 12.07191 12.04761 12.02300 11.99842 11.97421 11.95069 11.92820
## [249] 11.90707 11.88764 11.87024 11.85520 11.84285 11.83353 11.82823 11.82727
## [257] 11.82994 11.83557 11.84345 11.85289 11.86321 11.87370 11.88368 11.89245
## [265] 11.89932 11.90360 11.90934 11.92051 11.93621 11.95556 11.97771 12.00176
## [273] 12.02684 12.05208 12.07660 12.09952 12.11997 12.13707 12.14994 12.15771
## [281] 12.16565 12.17859 12.19499 12.21330 12.23199 12.24952 12.26433 12.27489
## [289] 12.27965 12.28127 12.28328 12.28545 12.28758 12.28944 12.29082 12.29151
## [297] 12.29129 12.28995 12.28726 12.28302 12.27700 12.26899 12.25878 12.24358
## [305] 12.22158 12.19402 12.16214 12.12720 12.09043 12.05309 12.01640 11.98163
## [313] 11.95001 11.92279 11.90121 11.88087 11.85695 11.83008 11.80091 11.77006
## [321] 11.73817 11.70589 11.67383 11.64265 11.61298 11.58544 11.56068 11.53934
## [329] 11.52204 11.50622 11.48921 11.47153 11.45373 11.43630 11.41978 11.40470
## [337] 11.39156 11.38091 11.37118 11.36068 11.34970 11.33853 11.32746 11.31678
## [345] 11.30679 11.29776 11.29000 11.28379 11.27943 11.27720 11.27739 11.28030
## [353] 11.28560 11.29268 11.30140 11.31164 11.32325 11.33611 11.35008 11.36504
## [361] 11.38084 11.39736 11.41447 11.43203 11.45203 11.47620 11.50404 11.53504
## [369] 11.56868 11.60445 11.64184 11.68034 11.71944 11.75863 11.79739 11.83521
## [377] 11.87159 11.90601 11.93796 11.96692 11.99608 12.02850 12.06351 12.10043
## [385] 12.13859 12.17732 12.21594 12.25378 12.29018 12.32445 12.35592 12.38392
## [393] 12.41156 12.44206 12.47485 12.50939 12.54513 12.58152 12.61800 12.65402
## [401] 12.68905 12.72251 12.75388 12.78259 12.80809 12.82984 12.84895 12.86693
## [409] 12.88386 12.89982 12.91488 12.92911 12.94258 12.95537 12.96756 12.97892
## [417] 12.98921 12.99844 13.00665 13.01386 13.02010 13.02538 13.02975 13.03321
## [425] 13.03579 13.03753 13.03844 13.03856 13.03790 13.03634 13.03374 13.03014
## [433] 13.02556 13.02002 13.01353 13.00614 12.99785 12.98869 12.97870 12.96788
## [441] 12.95626 12.94378 12.93037 12.91603 12.90078 12.88461 12.86754 12.84957
## [449] 12.83072 12.81099 12.79039 12.76893 12.74661 12.72345 12.69945 12.67468
## [457] 12.64919 12.62294 12.59591 12.56807 12.53939 12.50983 12.47938 12.44799
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 464)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.62304 11.63918 11.65515 11.67094 11.68653 11.70191 11.71706 11.73197
## [9] 11.74664 11.76103 11.77515 11.78898 11.80250 11.81570 11.82857 11.84109
## [17] 11.85325 11.86504 11.87644 11.88744 11.89803 11.90819 11.91791 11.92718
## [25] 11.93598 11.94429 11.95212 11.95943 11.96622 11.97248 11.97822 11.98346
## [33] 11.98824 11.99258 11.99651 12.00005 12.00323 12.00608 12.00862 12.01088
## [41] 12.01289 12.01468 12.01627 12.01769 12.01896 12.02011 12.02117 12.02217
## [49] 12.02313 12.02408 12.02451 12.02392 12.02237 12.01994 12.01668 12.01265
## [57] 12.00793 12.00256 11.99662 11.99016 11.98326 11.97597 11.96835 11.96048
## [65] 11.95240 11.94419 11.93591 11.92762 11.91938 11.91126 11.90332 11.89561
## [73] 11.88822 11.88119 11.87459 11.86849 11.86294 11.85801 11.85199 11.84330
## [81] 11.83225 11.81911 11.80418 11.78773 11.77005 11.75144 11.73217 11.71254
## [89] 11.69283 11.67332 11.65431 11.63608 11.61891 11.60310 11.58892 11.57667
## [97] 11.56664 11.55910 11.55435 11.55086 11.54698 11.54279 11.53836 11.53378
## [105] 11.52912 11.52446 11.51989 11.51548 11.51130 11.50744 11.50398 11.50100
## [113] 11.49857 11.49678 11.49569 11.49540 11.49598 11.49751 11.50006 11.50373
## [121] 11.50858 11.51469 11.52215 11.53103 11.54141 11.55337 11.56932 11.59071
## [129] 11.61622 11.64452 11.67427 11.70414 11.73280 11.75893 11.78118 11.80373
## [137] 11.83110 11.86254 11.89730 11.93463 11.97377 12.01396 12.05446 12.09451
## [145] 12.13336 12.17024 12.20442 12.23512 12.26160 12.28722 12.31543 12.34569
## [153] 12.37742 12.41007 12.44306 12.47583 12.50783 12.53848 12.56721 12.59348
## [161] 12.61670 12.63894 12.66241 12.68682 12.71187 12.73726 12.76270 12.78788
## [169] 12.81251 12.83629 12.85892 12.88010 12.89953 12.91693 12.93198 12.94575
## [177] 12.95943 12.97296 12.98624 12.99919 13.01174 13.02379 13.03526 13.04608
## [185] 13.05615 13.06540 13.07375 13.08111 13.08740 13.09254 13.09644 13.09902
## [193] 13.10020 13.09990 13.09803 13.09452 13.08966 13.08382 13.07698 13.06913
## [201] 13.06026 13.05037 13.03943 13.02744 13.01439 13.00027 12.98507 12.96877
## [209] 12.95137 12.93285 12.91226 12.88899 12.86356 12.83646 12.80823 12.77935
## [217] 12.75035 12.72174 12.69402 12.66330 12.62605 12.58334 12.53622 12.48576
## [225] 12.43303 12.37907 12.32495 12.27174 12.22049 12.17227 12.12815 12.08917
## [233] 12.05641 12.02329 11.98350 11.93849 11.88969 11.83855 11.78651 11.73500
## [241] 11.68548 11.63937 11.59813 11.56319 11.53600 11.51296 11.48968 11.46638
## [249] 11.44331 11.42070 11.39879 11.37782 11.35803 11.33965 11.32291 11.30806
## [257] 11.29533 11.28496 11.27718 11.27608 11.28400 11.29862 11.31759 11.33859
## [265] 11.35928 11.37732 11.39039 11.39614 11.40178 11.41523 11.43520 11.46041
## [273] 11.48959 11.52146 11.55475 11.58816 11.62043 11.65027 11.67642 11.69758
## [281] 11.71249 11.71986 11.72489 11.73312 11.74373 11.75591 11.76884 11.78174
## [289] 11.79377 11.80414 11.81204 11.81665 11.81717 11.81280 11.80179 11.78365
## [297] 11.75942 11.73013 11.69682 11.66051 11.62224 11.58304 11.54395 11.50600
## [305] 11.47022 11.43765 11.40933 11.38628 11.35995 11.32319 11.27909 11.23074
## [313] 11.18126 11.13373 11.09126 11.05695 11.03390 11.01759 11.00149 10.98572
## [321] 10.97038 10.95558 10.94142 10.92800 10.91544 10.90383 10.89328 10.88390
## [329] 10.87580 10.86907 10.86382 10.86029 10.85852 10.85833 10.85955 10.86199
## [337] 10.86547 10.86982 10.87485 10.88038 10.88623 10.89222 10.89818 10.90521
## [345] 10.91438 10.92546 10.93823 10.95246 10.96791 10.98435 11.00156 11.01931
## [353] 11.03737 11.05550 11.07348 11.09108 11.10808 11.12670 11.14880 11.17353
## [361] 11.20005 11.22750 11.25504 11.28184 11.30703 11.32978 11.35152 11.37417
## [369] 11.39763 11.42181 11.44661 11.47194 11.49771 11.52380 11.55014 11.57662
## [377] 11.60315 11.62963 11.65596 11.68206 11.70782 11.73314 11.75794 11.78212
## [385] 11.80558 11.82988 11.85627 11.88412 11.91285 11.94184 11.97051 11.99823
## [393] 12.02442 12.04847 12.07304 12.10081 12.13117 12.16352 12.19726 12.23179
## [401] 12.26651 12.30082 12.33412 12.36581 12.39529 12.42196 12.44522 12.46446
## [409] 12.48092 12.49625 12.51051 12.52375 12.53604 12.54742 12.55795 12.56769
## [417] 12.57669 12.58500 12.59269 12.59981 12.60620 12.61166 12.61622 12.61990
## [425] 12.62272 12.62468 12.62583 12.62616 12.62571 12.62449 12.62252 12.61981
## [433] 12.61639 12.61228 12.60750 12.60199 12.59571 12.58865 12.58080 12.57215
## [441] 12.56269 12.55244 12.54136 12.52949 12.51682 12.50336 12.48912 12.47408
## [449] 12.45824 12.44162 12.42420 12.40599 12.38699 12.36720 12.34661 12.32523
## [457] 12.30305 12.28008 12.25632 12.23176 12.20641 12.18026 12.15332 12.12558
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")